﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.KhenThuongSinhVienService;
namespace eTMS.Controllers{
public class KhenThuongSinhVienControllerBase : CommonController{
	public KhenThuongSinhVienControllerBase()
		: base()
	{	
		this.dao = new KhenThuongSinhVienServiceClient();
	}
	~KhenThuongSinhVienControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private KhenThuongSinhVienEntity oKhenThuongSinhVien;	
	public KhenThuongSinhVienEntity KhenThuongSinhVienObject{
		get{
			return oKhenThuongSinhVien;
		}
		set{
			oKhenThuongSinhVien=value;
		}
	}
	protected KhenThuongSinhVienServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSKhenThuongSinhVien(false);			
			return _Table;
		}
	}
	public void LoadAllDSKhenThuongSinhVien(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oKhenThuongSinhVien==null)return false;
		bool result=true;
		try{
			oKhenThuongSinhVien = dao.SaveRet(oKhenThuongSinhVien);
		}
		catch{
			result=false;
		}
		if(result){
			_ListKhenThuongSinhVien.Add(oKhenThuongSinhVien);
		}
		return result;
	}
	public bool Update(){
		if(oKhenThuongSinhVien==null)return false;
		return dao.Update(oKhenThuongSinhVien);
	}
	public bool Delete(){
		if(oKhenThuongSinhVien==null)return false;
		bool retvalue = dao.Delete(oKhenThuongSinhVien);
		if(retvalue){			
			_ListKhenThuongSinhVien.Remove(oKhenThuongSinhVien);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 KhenThuongSinhVien_ID){
		bool retvalue = dao.DeleteByID(KhenThuongSinhVien_ID);		
		if(retvalue){
			KhenThuongSinhVienEntity oDeleted=FindKhenThuongSinhVien(KhenThuongSinhVien_ID);
			if(oDeleted!=null){
				_ListKhenThuongSinhVien.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oKhenThuongSinhVien = new KhenThuongSinhVienEntity();
	}
    //Copy data from data row in grid to object
    public KhenThuongSinhVienEntity MapDataRowToObject(DataRow row){
		KhenThuongSinhVienEntity o = new KhenThuongSinhVienEntity();
		 if(!row.IsNull("SinhVien_ID"))
			o.SinhVien_ID=ConvertInt(row["SinhVien_ID"]);
		 if(!row.IsNull("NgayKhenThuong"))
			o.NgayKhenThuong=ConvertDateTime(row["NgayKhenThuong"]);
		 if(!row.IsNull("DanhHieuKhenThuong_ID"))
			o.DanhHieuKhenThuong_ID=(Int32)(row["DanhHieuKhenThuong_ID"]);
		 if(!row.IsNull("HocKy_ID"))
			o.HocKy_ID=(Int32)(row["HocKy_ID"]);
		 if(!row.IsNull("NamHoc_ID"))
			o.NamHoc_ID=(Int32)(row["NamHoc_ID"]);
		 if(!row.IsNull("KhenThuongSinhVien_ID"))
		    o.KhenThuongSinhVien_ID=(Int32)(row["KhenThuongSinhVien_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(KhenThuongSinhVienEntity o,DataRow row){
		row["SinhVien_ID"]=o.SinhVien_ID;
		row["NgayKhenThuong"]=o.NgayKhenThuong;
		row["DanhHieuKhenThuong_ID"]=o.DanhHieuKhenThuong_ID;
		row["HocKy_ID"]=o.HocKy_ID;
		row["NamHoc_ID"]=o.NamHoc_ID;
		row["KhenThuongSinhVien_ID"]=o.KhenThuongSinhVien_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.KhenThuongSinhVienObject,row);
    }
	public void LoadObject(Int32 KhenThuongSinhVien_ID){
		KhenThuongSinhVienEntity oLoad=FindKhenThuongSinhVien(KhenThuongSinhVien_ID);
		if(oLoad==null){
			oKhenThuongSinhVien=this.LoadKhenThuongSinhVien(KhenThuongSinhVien_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oKhenThuongSinhVien.IsNullKhenThuongSinhVien_ID;
			if(!bNotIsNull){
				this.ListKhenThuongSinhVien.Add(oKhenThuongSinhVien);
			}
		}
		else{
			oKhenThuongSinhVien=oLoad;
		}
	}
	#region CachedList KhenThuongSinhVien
	private static List<KhenThuongSinhVienEntity> _ListKhenThuongSinhVien=new List<KhenThuongSinhVienEntity>();
    public List<KhenThuongSinhVienEntity>ListKhenThuongSinhVien{
		get{
			return _ListKhenThuongSinhVien;
		}
	}
	public static List<KhenThuongSinhVienEntity>ListKhenThuongSinhVienStatic{
		get{
			return _ListKhenThuongSinhVien;
		}
	}
	public KhenThuongSinhVienEntity FindKhenThuongSinhVien(Int32 KhenThuongSinhVien_ID){
		KhenThuongSinhVienEntity oResult=this.ListKhenThuongSinhVien.Find(delegate(KhenThuongSinhVienEntity oFind){
			bool result=true; 
			result&=oFind.KhenThuongSinhVien_ID.Equals(KhenThuongSinhVien_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllKhenThuongSinhVien(bool clearCache){
		if(_ListKhenThuongSinhVien.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		KhenThuongSinhVienServiceClient dao = new KhenThuongSinhVienServiceClient();
		_ListKhenThuongSinhVien=new List<KhenThuongSinhVienEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public KhenThuongSinhVienEntity LoadKhenThuongSinhVien(Int32 KhenThuongSinhVien_ID){			
		KhenThuongSinhVienEntity o=dao.SelectOne(KhenThuongSinhVien_ID);		
		return o;
	}
	#endregion
}
}
